package io.modifier.basic.compose.hooks.userequest.utils

import kotlinx.datetime.Instant
import io.modifier.basic.compose.hooks.comm.TParams
import io.modifier.basic.compose.hooks.comm.cacheKey
import io.modifier.basic.compose.hooks.utils.currentTime

data class CachedData<TData>(
    val data: TData,
    val params: TParams = emptyArray(),
) {
    val time: Instant = currentTime

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || this::class != other::class) return false

        other as CachedData<*>

        if (data != other.data) return false
        if (!params.contentEquals(other.params)) return false
        return time == other.time
    }

    override fun hashCode(): Int {
        var result = data?.hashCode() ?: 0
        result = 31 * result + params.contentHashCode()
        result = 31 * result + time.hashCode()
        return result
    }
}

fun clearCache(vararg keys: String) {
    io.modifier.basic.compose.hooks.utils.CacheManager.clearCache(*keys.map { it.cacheKey }.toTypedArray())
}
